package com.yc.chaoshi.cs_order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yc.chaoshi.cs_order.DTO.GoodsAndAdress;
import com.yc.chaoshi.cs_order.DTO.OrderAllDTO;
import com.yc.chaoshi.cs_order.DTO.OrderGoodsDTO;
import com.yc.chaoshi.cs_order.mapper.OmsOrderProductMapper;
import com.yc.chaoshi.cs_order.mapper.PmsGoodsMapper;
import com.yc.chaoshi.cs_order.mapper.UmsAddresMapper;
import com.yc.chaoshi.cs_order.mapper.OmsOrderMapper;
import com.yc.chaoshi.cs_order.service.IOmsOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.yc.chaoshi.entity.OmsOrder;
import com.yc.chaoshi.entity.OmsOrderProduct;
import com.yc.chaoshi.entity.PmsGoods;
import com.yc.chaoshi.entity.UmsUserAddres;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xz
 * @since 2022-08-04
 */
@Service
public class OmsOrderServiceImpl extends ServiceImpl<OmsOrderMapper, OmsOrder> implements IOmsOrderService {
    @Resource
    UmsAddresMapper umsAddresMapper;
    @Resource
    PmsGoodsMapper pmsGoodsMapper;
    @Resource
    OmsOrderMapper omsOrderMapper;
    @Resource
    OmsOrderProductMapper orderProductMapper;
    @Override
    public List<OrderGoodsDTO> listAndGoods(int uid, String id) {
        return  this.baseMapper.listAndGoods(uid,id);
    }

    public GoodsAndAdress listbyId(int uid, String id) {
        List<OrderGoodsDTO> list=this.baseMapper.listAndGoods(uid,id);
        OrderGoodsDTO orderGoodsDTO=list.get(0);
        Integer addressid =orderGoodsDTO.getAdressId();
        UmsUserAddres umsAddres=umsAddresMapper.selectById(addressid);
        GoodsAndAdress goodsAndAdress=new GoodsAndAdress();
        BeanUtils.copyProperties(orderGoodsDTO,goodsAndAdress);
        goodsAndAdress.setGoods(orderGoodsDTO.getGoods());
        goodsAndAdress.setUmsAddres(umsAddres);
        return  goodsAndAdress;
    }

    /**
     * 根据购物车ids生成
     * @param goodsid
     * @return
     */
    public List<PmsGoods> ByIdsOrder(List<Integer> goodsid) {
          return pmsGoodsMapper.selectBatchIds(goodsid);
    }

    public Integer addorder(OrderAllDTO orderAllDTO) {
        UmsUserAddres userAddres=orderAllDTO.getAddress();
        userAddres.setUserId(orderAllDTO.getUid());
        umsAddresMapper.insert(userAddres);
        System.out.println(userAddres.getId());
        OmsOrder omsOrder=new OmsOrder();
        omsOrder.setAdressId(userAddres.getId());
        omsOrder.setConfirmStatus(0);
        omsOrder.setCreateTime(LocalDateTime.now());
        omsOrder.setStatus(1);
        omsOrder.setDeliveryWay(orderAllDTO.getDelivery_way());
        omsOrder.setUserId(orderAllDTO.getUid());
        omsOrder.setSumCount(orderAllDTO.getPrice());
        omsOrderMapper.insert(omsOrder);
        List<PmsGoods> goodsList=orderAllDTO.getGoodids();
        for (int i = 0; i < goodsList.size(); i++) {
            OmsOrderProduct omsOrderProduct=new OmsOrderProduct();
            omsOrderProduct.setOrderId(omsOrder.getId());
            omsOrderProduct.setProductId(goodsList.get(i).getId());
            omsOrderProduct.setCout(goodsList.get(i).getSales());
            orderProductMapper.insert(omsOrderProduct);
        }
      return omsOrder.getId();
    }

    public OmsOrder ById(Integer uid, String orderid) {
        QueryWrapper<OmsOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(OmsOrder::getId,orderid)
                             .eq(OmsOrder::getUserId,uid);
        return  this.baseMapper.selectOne(queryWrapper);
    }
}
